using System;
using System.Threading.Tasks.Dataflow;

namespace NewTry
{
    public class CompareBlock
    {
        public Action<(bool, int)> DataArrived;
        public ActionBlock<Tuple<byte[], byte[]>> compareBlock;
        public JoinBlock<byte[], byte[]> jb = new JoinBlock<byte[], byte[]>();
        private int framenumber=0;

        public CompareBlock()
        {
            // 初始化 compareBlock
            compareBlock = new ActionBlock<Tuple<byte[], byte[]>>((input) =>
            {

                framenumber++;
                //Console.WriteLine(input.Item1 + "compare " + input.Item2);
                if (input.Item1.Length != input.Item2.Length)
                {
                    DataArrived?.Invoke((false, framenumber));
                }

                for (int i = 0; i < input.Item1.Length; i++)
                {
                    if (input.Item1[i] != input.Item2[i])
                    {
                        DataArrived?.Invoke((false, framenumber));
                    }
                }
            }, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 1 }); // 确保顺序执行
            // 链接 jb 和 compareBlock
            jb.LinkTo(compareBlock, new DataflowLinkOptions { PropagateCompletion = true });
        }

        public void Enqueue_1(byte[] input1)
        {
            jb.Target1.Post(input1); // 文件1的数据帧
        }

        public void Enqueue_2(byte[] input2)
        {
            jb.Target2.Post(input2); // 文件2的数据帧
        }



        
    }
}